<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Models\Answer;
use App\Models\Question;

use App\Models\UserDetail;
class AnswerController extends CommonController
{
    /**
     *
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * 返回回复问题页面
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {

        //获取问题的id
        $id = $request->Input('qid');

        // 当前问题的访问量加一
         Question::where('id',$id)->increment('view');

        //查询该条问题记录
        $data = DB::table('zy_question')->find($id);

        //获取用户的id
        $uid = $data['uid'];

        // 查询问题用户信息
        $q_user = DB::table('zy_userdetails')->where('uid',$uid)->first();
        // 查询已回复问题数
        $count_que = count(DB::table('zy_question')->where('count','>','0')->get());

        // 获取答案最多的热搜问题 6条 并获取问题的分类名称
        $category = DB::table('zy_category') ->join('zy_question','zy_category.id','=','zy_question.cate_id')->orderBy('count','desc')->take(8)-> get();


        return view('home.answer.create',compact('data','q_user','category','count_que'));
    }

    /**
     * 处理回复问题
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $answer = $request->except('_token');
        $a_time = $answer['a_time'];
        $a_content = $answer['a_content'];
        $uid = session('login');
        $qid = $answer['qid'];
        $pid=$answer['pid'];
        $path=$answer['path'];
        if($a_content == null){

           return back()->with('error','不能提交空数据');

        }
        $res = DB::table('zy_answer')->insertGetId([
                    'uid'=>$uid,
                    'a_content'=>$a_content,
                    'a_time'=>$a_time,
                    'qid'=>$qid,
                    'pid'=>$pid,
                    'path'=>$path
            ]);

        if($res)
           {


                // 回复成功加积分
                $score = DB::table('zy_score')->where('uid',$uid)->first();

                $score1 = $score['score']+5;
                // 回复成功加经验
                $exp = $score['exp']+10;
                //修改
                $re = DB::table('zy_score')->where('uid',$uid)->update(['score'=>$score1,'exp'=>$exp]);
                Question::where('id',$qid)->increment('count');
                //用户详情表统计这个用户的问题回答量
                Userdetail::where('uid',$uid)->increment('a_count');

                return view('home.answer.jump')->with([
                    'message'=>'提交成功,经验+10,积分+5',
                    'url' =>'/home/answer/'.$qid,
                    'jumpTime'=>5,
                ]);

           }else{
            return back()->with('error','回复失败');
           }
    }

    /**
     * 返回问题回复列表页
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //问题id
        $qid = $id;
        //问题表去获得用户id
        $question = DB::table('zy_question')->where('id',$qid)->first();
        $q_uid = $question['uid'];
        $q_userdetail = DB::table('zy_userdetails')->where('uid',$q_uid)->first();

        $answer = DB::table('zy_answer')->where('qid',$qid)->where('pid','0')->get();

        $arr1=array();
        $arr1=$this->getCommlist();//获取评论列表
        // dd($arr1);
        $arr=[];

        foreach($arr1 as $k=>$v)
        {

               if($v['level'] >= 0 && $v['level'] <= 1)
               {
                       $v['level'] = "倔强青铜";
               }elseif($v['level'] > 1 && $v['level'] <= 2)
               {
                       $v['level'] = "秩序白银";
               }elseif($v['level'] > 2 && $v['level'] <= 3)
               {
                       $v['level'] = "荣耀黄金";
               }elseif($v['level'] > 3 && $v['level'] <= 4)
               {
                       $v['level'] = "尊贵铂金";
               }elseif($v['level'] > 4 && $v['level'] <= 5)
               {
                       $v['level'] = "永恒钻石";
               }elseif($v['level'] > 5 && $v['level'] <= 6)
               {
                       $v['level'] = "至尊星耀";
               }elseif($v['level'] > 6)
               {
                       $v['level'] = "最强王者";
               }

               $arr[] = $v;
        }
         // 获取答案最多的热搜问题 6条 并获取问题的分类名称
        $category = DB::table('zy_category') ->join('zy_question','zy_category.id','=','zy_question.cate_id')->orderBy('count','desc')->take(8)-> get();
        // 查询已回复问题数
        $count_que = count(DB::table('zy_question')->where('count','>','0')->get());
        return view('home.answer.show',compact('arr','question','q_userdetail','qid','category','count_que'));

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 处理回复删除
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = [];
        $res = DB::table('zy_answer')->where('a_id','=',$id)->where('uid',session('login'))->update(['a_status'=>'1']);

        //删除成功
        if($res){
            $data['status']= 0;
            $data['msg']='删除成功';
        }else{
           $data['status']= 1;
           $data['msg']='删除失败，您没有权限';
        }
        return $data;
    }


    /**
     *递归获取评论列表
     */
    protected function getCommlist($pid = 0,&$result = array()){
          //join('要链接的表', '当前表跟连接表关联的字段', '=', '被链接表.外键')
        $arr = DB::table('zy_answer')->join('zy_userdetails','zy_answer.uid','=','zy_userdetails.uid')->where('a_status','0')->where('pid',$pid)->orderBy('a_time','desc')->get();
        if(empty($arr)){
            return array();
        }
        foreach ($arr as $cm) {
            $thisArr=&$result[];
            $cm["children"] = $this->getCommlist($cm["a_id"],$thisArr);
            $thisArr = $cm;
        }
        return $result;
    }

    //前台回复的盖楼
    public function show_detail()
    {
        echo 1;
    }
}
